<?php 
include_once '../classes/design.php';
include_once '../classes/dao.php';

$design = new Design("..", "onbeforeunload='return confirmarSaida()'");
$design->adicionar_form("form_musica");
$dao = new DAO();

$musica = new Musica();
if (isset($_REQUEST["id"])) {
	try {
		$musica = $dao->get_musica($_REQUEST["id"]);
	}
	catch (Exception $e) {
		header("Location: index.php?erro=".$e->getMessage());
	}
}

$design->imprimir_topo();
?>

<a href="index.php" class="titulo">Músicas</a><br/>

<script type="text/javascript">
var contador = 0;

function adicionarLink() {
	contador++;
	var wrapper = document.getElementById("novos_links");
	var novoLink = document.createElement("div");
	novoLink.setAttribute("id", "novo_link_" + contador);
	novoLink.innerHTML = "Tipo: <input type=\"text\" onfocus=\"javascript: selecionar(this);\" onblur=\"javascript: desselecionar(this);\" name=\"tipos_novos_links[]\" id=\"tipo_novo_link_" + contador + "\" style=\"width: 100px;\" /> Endereço: <input type=\"text\" name=\"urls_novos_links[]\" style=\"width: 300px;\" onfocus=\"javascript: selecionar(this);\" onblur=\"javascript: desselecionar(this);\"/> <a href=\"javascript: void(0);\" onclick=\"javascript: removerLink(" + contador + ");\">Cancelar</a><br/>";
	wrapper.appendChild(novoLink);
	$("#tipo_novo_link_" + contador).autocomplete({ 
		source: '../servicos/procurar_tipos_links.php',
		minLength: 3, 
		delay: 0
	});
}

function removerLink(id) {
	var wrapper = document.getElementById("novos_links");
	var link = document.getElementById("novo_link_" + id);
	wrapper.removeChild(link); 
}
</script>

<script type="text/javascript" src="../javascript/validacao.js"></script>
<script type="text/javascript" src="../javascript/funcoes.js"></script>
<script type="text/javascript" src="../javascript/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="../javascript/jquery-ui-1.8.17.custom.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
	var options = {
			source: "../servicos/procurar_tags.php",
			minLength: 2,
			delay: 0,
		}
	$("#tag_0").autocomplete(options);
	$("#tag_1").autocomplete(options);
	$("#tag_2").autocomplete(options);
	$("#tag_3").autocomplete(options);
	$("#tag_4").autocomplete(options);
	$("#de_quem_eh").autocomplete({
		source: '../servicos/procurar_de_quem_eh.php',
		minLength: 3, 
		delay: 0
	});
});
</script>

<?php 
if (isset($erro)) {
	echo "<div class=\"erro\">$erro</div>";
}

echo "
 <form method=\"post\" action=\"salvar.php\" id=\"form_musica\">
	<input type=\"hidden\" name=\"id\" value=\"".$musica->get_id()."\" />
	<table border=\"0\" align=\"center\">
		<tr valign=\"top\">
			<td>
				<table border=\"0\">
";
if ($musica->get_quem_mexeu() != "") {
	echo "
					<tr valign=\"top\">
						<td align=\"right\" colspan=\"2\"><i>Última alteração: ".$musica->get_quando_mexeu()." por ".$musica->get_quem_mexeu()."</i></td>
					</tr>
	";
}
echo "
					<tr valign=\"top\">
						<td align=\"right\"><b>Título: *</b></td>
						<td><input type=\"text\" name=\"titulo\" value=\"".htmlentities($musica->get_titulo(), ENT_QUOTES, "UTF-8")."\" style=\"width: 100%;\" alt=\"Título|Obrig\" />
					</tr>
					<tr valign=\"top\">
						<td align=\"right\"><b>De quem é:</b></td>
						<td><input type=\"text\" id=\"de_quem_eh\" name=\"de_quem_eh\" value=\"".htmlentities($musica->get_de_quem_eh(), ENT_QUOTES, "UTF-8")."\" style=\"width: 100%;\" />
					</tr>
					<tr valign=\"top\">
						<td align=\"right\"><b>Letra:</b></td>
						<td><textarea name=\"letra\" style=\"width: 100%; height: 400px;\">".$musica->get_letra()."</textarea></td>
					</tr>
					<tr valign=\"top\">
						<td align=\"right\"><b>Tom padrão:</b></td>
						<td>
							<select name=\"tom_padrao\">
";
foreach (Musica::lista_tons() as $tom) {
	echo "<option value=\"$tom\"";
	if ($musica->get_tom_padrao() == $tom) {
		echo " selected";
	}
	echo ">$tom</option>";
}
echo "
							</select>
						</td>
					</tr>
					<tr valign=\"top\">
						<td align=\"right\"><b>Links:</b></td>
						<td>
							<table class=\"semPadding\">
";
foreach ($musica->get_links() as $link) {
	echo "
								<tr>
									<td>".$link->get_tipo()." - <a href=\"".$link->get_url()."\" target=\"_blank\">".$link->get_url_truncado(30)."</a></td>
									<td>
										<input type=\"checkbox\" name=\"deletar_links[]\" value=\"".$link->get_id()."\" id=\"deletar_link_".$link->get_id()."\" />
										<label for=\"deletar_link_".$link->get_id()."\">Deletar</label>
									</td>
								</tr>
	";
}
echo "
							</table>
							<div id=\"novos_links\"></div>
							<a href=\"javascript: void(0);\" onclick=\"javascript: adicionarLink();\">".$design->get_imagem("mais.gif")." Adicionar link</a>
						</td>
					</tr>
					<tr valign=\"top\">
						<td align=\"right\"><b>Tags:</b></td>
						<td>
							<table class=\"semPadding\">
";
foreach ($musica->get_tags() as $tag) {
	echo "
								<tr>
									<td>".$tag->get_tag()."</td>
									<td>
										<input type=\"checkbox\" name=\"remover_tags[]\" value=\"".$tag->get_id()."\" id=\"remover_tag_".$tag->get_id()."\" />
										<label for=\"remover_tag_".$tag->get_id()."\">Remover</label>
									</td>
								</tr>
	";
}
echo "
							</table>
							Adicionar tags:<br/>
";
for ($i = 0; $i < 5; $i++) {
	echo "<input type=\"text\" id=\"tag_$i\" name=\"novas_tags[]\" style=\"width: 120px; margin-right: 5px;\"/>";
}
echo "
						</td>
					</tr>
					<tr valign=\"top\">
						<td align=\"right\"><b>Observações:</b></td>
						<td><textarea name=\"observacoes\" style=\"width: 100%; height: 50px;\">".$musica->get_observacoes()."</textarea></td>
					</tr>
					<tr>
						<td colspan=\"2\" align=\"center\">
							<button type=\"submit\">Salvar</button>
							<button type=\"button\" onclick=\"javascript: window.location.href='index.php'\">Cancelar</button>
";
if ($musica->get_id() != "") {
	echo "<button type=\"button\" onclick=\"javascript: confirmarExclusaoMusica(".$musica->get_id().", '".addslashes(htmlspecialchars($musica->get_titulo()))."');\">Excluir</button>";	
}
echo "
						</td>
					</tr>
				</table>
			</td>
";

if ($musica->get_id() != "") {
	$missas = $dao->consulta_eventos(array("Missa"), "", "", "", "", "", $musica->get_id(), "", "data_inicio", "DESC");
	$animacoes = $dao->consulta_eventos(array("Animação"), "", "", "", "", "", $musica->get_id(), "", "data_inicio", "DESC");
	if (count($missas) + count($animacoes) > 0) {
		echo "
				<td>
		";
		if (count($missas) > 0) {
			echo "
				<div class=\"lateral\">
			 		<b>Missas onde foi tocada:</b><p/>
			";
			$separador = "";
			foreach ($missas as $missa) {
				$musicas_missa = $missa->get_musicas_missa();
				$tom = "";
				foreach ($musicas_missa as $musica_missa) {
					if (!is_null($musica_missa) && $musica_missa->get_musica()->get_id() == $musica->get_id()) {
						$tom = $musica_missa->get_tom();
						break;
					}
				}
				echo $separador."- <a href=\"../eventos/form_missa.php?id=".$missa->get_id()."\"/>".$missa->get_nome()."</a><br/>(Tom: ".$tom.")";
				$separador = "<p/>";
			}
			echo "
				</div>
				<p/>
			";
		}
		if (count($animacoes) > 0) {
			echo "
				<div class=\"lateral\">
				 	<b>Animações onde foi tocada:</b><p/>
				";
			$separador = "";
			foreach ($animacoes as $animacao) {
				$musicas_animacao = $animacao->get_musicas_animacao();
				$tom = "";
				$quem_canta = "";
				foreach ($musicas_animacao as $musica_animacao) {
					if (!is_null($musica_animacao) && $musica_animacao->get_musica()->get_id() == $musica->get_id()) {
						$tom = $musica_animacao->get_tom();
						$quem_canta = $musica_animacao->get_quem_canta();
						break;
					}
				}
				echo $separador."- <a href=\"../eventos/form_animacao.php?id=".$animacao->get_id()."\">".$animacao->get_nome()."</a><br/>(".($quem_canta != "" ? "Cantada por ".$quem_canta : "")."; Tom: ".$tom.")";
				$separador = "<p/>";
			}
			echo "
					</div>
			";
		}
		echo "		 		
			 	</td>
		";
	}
}
echo "
		</tr>
	</table>
 </form>
";
?>


<?php 
$design->imprimir_fim();
?>